﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TVDL4.VO;
using TVDL4.BUS;

namespace TVDL4
{
    public partial class frmThiNghiem : frmParent
    {
        private ThiNghiemBUS thinghiemBUS;
        private Module.ClickButtonStatus flagButton = Module.ClickButtonStatus.Ngung;//co phan biet dang o trang thai ghi hay ngung
        public frmThiNghiem()
        {
            InitializeComponent();
            thinghiemBUS = new ThiNghiemBUS();
        }

        #region "event Method"
        private void frmThinghiem_Load(object sender, EventArgs e)
        {
            LoadCongViecToCombo();
            LoadDataOnFlex();
        }

        #endregion

        #region "Private Method"
        private void ClearControl()
        {
            txtThiNghiem.Text = "";
            cboVatLieu.SelectedText = null;
        }
        private void UpdateControl(int index)
        {
            flexThiNghiem.Rows[index]["ThiNghiem"] = txtThiNghiem.Text;
            flexThiNghiem.Rows[index]["VATLIEU"] = cboVatLieu.Text;
            flexThiNghiem.Rows[index]["KEYSLVATLIEU"] = cboVatLieu.SelectedValue;
           
        }
        private void StatusButton()
        {
            if (flagButton == Module.ClickButtonStatus.Them || flagButton == Module.ClickButtonStatus.Sua)
            {
                btnThem.Text = "&Ghi";
                btnSua.Text = "&Ngưng";
                btnXoa.Enabled = false;
                btnThoat.Enabled = false;
                flexThiNghiem.Enabled = false;
                txtThiNghiem.Focus();

            }
            else
            {
                btnThem.Text = "&Thêm";
                btnSua.Text = "&Sửa";
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                flexThiNghiem.Enabled = true;

            }
        }
        private void LoadCongViecToCombo()
        {
            VatLieuBUS vatlieuBUS = new VatLieuBUS();
            cboVatLieu.ClearItems();
            cboVatLieu.DefColWidth = 100;
            cboVatLieu.AllowSort = true;
            cboVatLieu.DataSource = vatlieuBUS.GetListVatLieu();
            cboVatLieu.Columns[0].DataWidth = 0;


            //cboVatLieu.Columns[1].Caption = "vatlieu";
            
        }
        private void LoadDataOnFlex()
        {
            flexThiNghiem.Rows.Count = 1;
            List<ThiNghiemVO> lstThiNghiem = thinghiemBUS.GetListThiNghiem();
            foreach (ThiNghiemVO thinghiemVO in lstThiNghiem)
            {
                flexThiNghiem.Rows.Count++;
                flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["STT"] = flexThiNghiem.Rows.Count - 1;
                flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["KEYSL"] = thinghiemVO.KeySL;
                flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["ThiNghiem"] = thinghiemVO.TenThiNghiem;
                flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["KEYSLVATLIEU"] = thinghiemVO.LoaiVatlieu.KeySL;
                flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["VATLIEU"] = thinghiemVO.LoaiVatlieu.TenVatLieu;
            }
        }

        #endregion

        private void flexThiNghiem_AfterRowColChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)
        {
            if (flexThiNghiem.Selection.TopRow >= 1 && flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["ThiNghiem"] != null)
            {
                txtThiNghiem.Text = flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["ThiNghiem"].ToString();
                cboVatLieu.SelectedValue = flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["KEYSLVATLIEU"].ToString();
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (flexThiNghiem.Selection.TopRow >= 1 && flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["KEYSL"].ToString() != "")
            {
                if (thinghiemBUS.DeleteRecord(flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["KEYSL"].ToString()))
                {
                    flexThiNghiem.Rows.Remove(flexThiNghiem.Selection.TopRow);
                    ClearControl();
                }

            }
        }

        public  void btnThem_Click(object sender, EventArgs e)
        {
            ThiNghiemVO ThiNghiemVO;
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:
                    flagButton = Module.ClickButtonStatus.Them;
                    ClearControl();
                    break;
                case Module.ClickButtonStatus.Them:
                    ThiNghiemVO = new ThiNghiemVO(null, txtThiNghiem.Text, new VatLieuVO(cboVatLieu.SelectedValue.ToString(),""));
                    ThiNghiemVO = thinghiemBUS.InsertRecord(ThiNghiemVO);
                    if (ThiNghiemVO == null)
                        MessageBox.Show("Không ghi được dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    else
                    {
                        flexThiNghiem.Rows.Count++;
                        flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["STT"] = flexThiNghiem.Rows.Count - 1;
                        flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["KEYSL"] = ThiNghiemVO.KeySL;
                        flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["ThiNghiem"] = ThiNghiemVO.TenThiNghiem;
                        flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["KEYSLVATLIEU"] = ThiNghiemVO.LoaiVatlieu.KeySL;
                        flexThiNghiem.Rows[flexThiNghiem.Rows.Count - 1]["VatLieu"] = cboVatLieu.Text; ;
                        //ClearControl();
                    }
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
                case Module.ClickButtonStatus.Sua:
                    ThiNghiemVO = new ThiNghiemVO(flexThiNghiem.Rows[flexThiNghiem.Selection.TopRow]["KEYSL"].ToString(), txtThiNghiem.Text, new VatLieuVO(cboVatLieu.SelectedValue.ToString(), ""));
                    thinghiemBUS.UpdateRecord(ThiNghiemVO);
                    UpdateControl(flexThiNghiem.Selection.TopRow);
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;


            }
            StatusButton();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {

            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:
                    flagButton = Module.ClickButtonStatus.Sua;
                    break;
                case Module.ClickButtonStatus.Sua:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
                case Module.ClickButtonStatus.Them:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;

            }
            StatusButton();
        }
    }
}
